
var extraCallback = "";

var doAjaxLogin = function(callback, target) {
		setCallback(callback);
		$("#tan_bg").show();
		$("#tan_box").show();
		flushLoginTicket();
};

//设置登录后的回调方法
var setCallback = function(callbackName) {
	extraCallback = callbackName;
};

// 登录验证函数, 由 onsubmit 事件触发  
var loginValidate = function() {
    var msg = '';  
    if ($.trim($('#J_Username').val()).length == 0 ){  
        msg = "用户名不能为空。";  
    } else if ($.trim($('#J_Password').val()).length == 0 ){  
        msg = "密码不能为空。";  
    }  
    if (msg != '' && msg.length > 0) {  
        $('#J_ErrorMsg').fadeOut().text(msg).fadeIn();  
        return false;  
        // Can't request the login ticket.  
    } else if ($('#J_LoginTicket').val().length == 0){  
        $('#J_ErrorMsg').text('服务器正忙，请稍后再试..');  
        return false;  
    } else {  
	    deleteIFrame('#ssoLoginFrame');
        // 验证成功后，动态创建用于提交登录的 iframe
		var _iframe;
		try { // for I.E.
			_iframe= document.createElement('<iframe name="ssoLoginFrame">');
		} catch (ex) { //for other browsers, an exception will be thrown
			_iframe = document.createElement('iframe'); 
		}
		_iframe.setAttribute("id","ssoLoginFrame");
		_iframe.setAttribute("name","ssoLoginFrame");
		_iframe.setAttribute("src","javascript:false;");
		_iframe.setAttribute("style","display:none;width:0;height:0");
		document.body.appendChild(_iframe); 
        return true;  
    }
};  
  
//创建应用前的ajax登录回调函数
var newAppLoginCallBack = function(result) {
	loginStatusCallback(result);
	if (result.login == 'fails') {
		return;
	}
	//登录成功查询应用个数	
	$.ajax({
		url:'../app/checkAppNum.do',
		type:"post",
		success:function(msg){
			if(msg==='false'){
				$('.jyd_info1').html('您创建的应用已满最高限额50，请进入我的app中删除闲置应用后在创建');
				$('#bkground').show();
				$('#publish').show();
				return;
			}else{
				$('#blackdiv').show();//显示应用名称弹出层
				$('#inputName').show();
				$('#appName').focus();
			}
		},
		error:function(){
			alert('查询应用数出错');
		}
	});
};  

//新手体验输入网址后的ajax登录回调函数
var newerLoginCallBack = function (result) {
	loginStatusCallback(result);
	if (result.login == 'fails') {
		return;
	}
	document.getElementById('create-app').submit();
};

//每个页面都调用loginStatus获取登录信息
var refreshLoginStatus = function (result) {
	if(result.login=='ok') {
		$('#loginSucc').show();
		$('#Cnickname').html(result.name);
		$('#loginFail').hide();
		if(result.msg_cnt=='0'){
			$('#message').html('消息');
		}else{
			$('#message').html('消息'+'<strong style="color:red;">'+result.msg_cnt+'</strong>');
		}
	}
    deleteIFrame('#loginStatusFrame');
    return;
};

//appcan网站loginStatus的回调方法
var loginStatusCallback = function (result) {
	if(result.login=='ok') {
		$('#loginSucc').show();
		$('#Cnickname').html(result.name);
		$('#loginFail').hide();
		if(result.msg_cnt=='0'){
			$('#message').html('消息');
		}else{
			$('#message').html('消息'+'<strong style="color:red;">'+result.msg_cnt+'</strong>');
		}
		$("#tan_bg").hide();
		$("#tan_box").hide();
	    deleteIFrame('#ssoLoginFrame');
	}else{
		$('#J_ErrorMsg').fadeOut().text('用户名或密码错').fadeIn();
	    flushLoginTicket();
	}
};

var deleteIFrame = function (iframeName) {  
    var iframe = $(iframeName);   
    if (iframe) { // 删除用完的iframe，避免页面刷新或前进、后退时，重复执行该iframe的请求  
        iframe.remove();
    }  
};  
  
//由于一个 login ticket 只允许使用一次, 当每次登录需要调用该函数刷新 lt
var flushLoginTicket = function() {
	var currentLoc = $('#loginServiceId').val();
	var _services = 'service=' + encodeURIComponent(currentLoc);

	$.ajax({
		url: "http://sso.tx100.com/login?callback=?",
		dataType: 'json',
		cache: false,
		async: false,
		data: _services+'&get-lt=true',
		success: function(data, textStatus, jqXHR) {
			$('#J_LoginTicket').attr("value",data.retData);
		},
		error: function(jqXHR, textStatus, errorThrown) {
			$("#tan_bg").hide();
			$("#tan_box").hide();
		}
	}); 
};


var doLoginCallback = function(result) {
	//执行真正的回调方法
	if (extraCallback != '') {
		eval(extraCallback + '(result)');
	}
	extraCallback = '';
    return;
};

var isEmpty = function(text_0) {
	return text_0 == null||text_0 == 'null' || (String(text_0)).replace(/\s*/g, "").length <= 0;
};
